package cn.tj212.xiaoyin.wechat.controller;

import cn.tj212.xiaoyin.common.page.TailPage;
import cn.tj212.xiaoyin.common.web.SessionContext;
import cn.tj212.xiaoyin.core.user.domain.UserCourseSection;
import cn.tj212.xiaoyin.core.user.domain.UserCourseSectionDto;
import cn.tj212.xiaoyin.core.user.service.IUserCourseSectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserCourseSectionService userCourseSectionService;

    /**
     * 个人主页
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request, TailPage<UserCourseSectionDto> page){
        ModelAndView mv=new ModelAndView("user");
        //当前用户ID
        Long userId= SessionContext.getWxUserId(request);
        if (null==userId){
            return new ModelAndView("redirect:/auth/login.html");
        }
        //获取学习记录
        UserCourseSection queryEntity=new UserCourseSection();
        queryEntity.setUserId(userId);

        page=userCourseSectionService.queryPage(queryEntity,page);
        mv.addObject("page",page);
        //当前用户
        mv.addObject("curUser",SessionContext.getWxAuthUser(request));
        return mv;
    }
}
